package com.scse.bms.util;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * @Title: VerifyImgCodeUtil
 * @Author: 吴赠禹
 * @Date: 2020/10/26 16:44
 * @Description: 图片验证码生成器
 */

@Slf4j
public class VerifyImgCodeUtil {

    private static final int width = 178;
    private static final int height = 50;

    private static final String SAMPLE = "qwertyuiopasdfghjklzxcvbnm1234567890ZXCVBNMASDFGHJKLQWERTYUIOP";

    /**
     * 获取四个随机字符
     * @return
     */
    private static String get4char() {
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(SAMPLE.length());
            buffer.append(SAMPLE.charAt(index));
        }
        return buffer.toString();
    }

    /**
     * 在内存中绘制验证码图片
     * @param response
     * @return
     * @throws IOException
     */
    public static String generateImgCode(HttpServletResponse response) throws IOException {
        // 步骤一：绘制一张内存中的图片
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 步骤二：图片绘制背景颜色
        Graphics graphics = bufferedImage.getGraphics();
        // 绘制之前指定颜色
        graphics.setColor(getRandColor(200, 250));
        graphics.fillRect(0, 0, width, height);
        // 步骤三：绘制边框
        graphics.setColor(Color.WHITE);
        graphics.drawRect(0, 0, width - 1, height - 1);
        // 步骤四：四个随机符
        Graphics2D graphics2D = (Graphics2D) graphics;
        // 设置输出字体
        graphics2D.setFont(new Font("YaHei", Font.BOLD, 46));
        // 获取随机字符
        String word = get4char();
        Random random = new Random();
        // 定义x坐标
        int x = 10;
        for (int i = 0; i < word.length(); i++) {
            // 随机颜色
            graphics2D.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            // 旋转度数
            int angle = random.nextInt(30) - 15;
            // 换算弧度
            double theta = angle * Math.PI / 180;
            // 获取字符
            char c = word.charAt(i);
            // 旋转写字
            graphics2D.rotate(theta, x, 33);
            graphics2D.drawString(String.valueOf(c), x, 40);
            // 旋转回来，否则一直往同一个方向转
            graphics2D.rotate(-theta, x, 33);
            x += 42;
        }
        // 打印验证码
        log.warn("[VerifyImgCodeUtil] generateImgCode --> " + word);
        // 步骤五：绘制干扰线
        int x1, x2, y1, y2;
        for (int i = 0; i < 30; i++) {
            x1 = random.nextInt(width);
            x2 = random.nextInt(12);
            y1 = random.nextInt(height);
            y2 = random.nextInt(12);
            graphics.drawLine(x1, y1, x1 + x2, x2 + y2);
        }
        // 释放资源
        graphics.dispose();
        // 将图片写入输出流
        ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
        return word;
    }

    /**
     * 功能描述: 取随机颜色
     * @param: [fc, bc]
     * @return: java.awt.Color
     * @auther: 吴赠禹
     * @date: 2020/10/26  下午 4:55
     */
    private static Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }
}
